package top.infopub.job.cap.service.impl;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import top.infopub.job.cap.dao.LoanContractInterestDao;
import top.infopub.job.cap.service.LoanContractInterestService;
import top.infopub.model.cap.LoanContractSublist;
import top.infopub.model.cap.ResultModel;

import java.math.BigDecimal;
import java.util.List;


/**
 * @Author Warking
 * @Date 2021/6/6 13:59
 * @Version 1.0
 */
@Service
public class LoanContractInterestServiceImpl  implements LoanContractInterestService {
    @Autowired
    private LoanContractInterestDao loanContractInterestDao;

    @Override
    public List<LoanContractSublist> selectRateList() {
        return loanContractInterestDao.selectRateList();
    }

    @Override
    public void updateInterestRate(String id, BigDecimal y, BigDecimal conFinCost) {
         loanContractInterestDao.updateInterestRate(id,y, conFinCost);
    }

    @Override
    @Transactional
    public void updateInterestRate() {
        /*获取数据*/
        String url = "http://www.chinamoney.com.cn/r/cms/www/chinamoney/data/currency/bk-lpr.json";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity requestBody = new HttpEntity(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestBody, String.class);
        String result = responseEntity.getBody();
        ResultModel resultModel = JSON.parseObject(result, ResultModel.class);
        List<ResultModel.RecordsBean> resultModelList = resultModel.getRecords();
        String y1 = resultModelList.get(0).getShibor();
        String y5 = resultModelList.get(1).getShibor();

        /*写service     查询的sql得到未到期日期的数据List */
        List<LoanContractSublist> domainList = selectRateList();
        /*遍历数据  通过接口得到最新的利率 然后比较利率是不是一样的  不一样就修改*/
        for (LoanContractSublist loanContract : domainList) {
            //判断 利率方式为2 就是选的浮动利率 而且FLOATING_RATE_MODE浮动利率类型选LPR = 2
            if ("2".equals(loanContract.getInterestRateMode()) && "2".equals(loanContract.getFloatingRateMode())) {
                BigDecimal tmpBd = new BigDecimal(loanContract.getLprBp()).multiply(new BigDecimal("0.01"));
                if ("1".equals(loanContract.getLprMode())) { //一年期LPR
                    //判断一年期的利率相不相同
                    if (!y1.equals(loanContract.getInterestRate())) {
                        tmpBd = tmpBd.add(new BigDecimal(y1));
                        //更新子表数据
                        updateInterestRate(loanContract.getId(), new BigDecimal(y1), tmpBd);
                        //更新主表利率
                        loanContractInterestDao.updateFinCost(loanContract.getLoanContractId(), tmpBd.toString());
                    }
                } else if ("2".equals(loanContract.getLprMode())) { //五年期LPR
                    //判断五年期的利率相不相同
                    if (!y5.equals(loanContract.getInterestRate())) {
                        tmpBd = tmpBd.add(new BigDecimal(y5));
                        //更新子表数据
                        updateInterestRate(loanContract.getId(),new BigDecimal(y5), tmpBd);
                        //更新主表利率
                        loanContractInterestDao.updateFinCost(loanContract.getLoanContractId(), tmpBd.toString());
                    }
                }
            }
        }
    }
}
